package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.pop.PopMenuTimeLineTop;
import com.github.postsanf.yinian.activity.pop.PopShareTimeLine;
import com.github.postsanf.yinian.adapter.DetailAdapter;
import com.github.postsanf.yinian.adapter.TimeLineDetialAdapter;
import com.github.postsanf.yinian.adapter.ZanHistoryAdapter;
import com.github.postsanf.yinian.bean.YNComment;
import com.github.postsanf.yinian.bean.YNLike;
import com.github.postsanf.yinian.bean.YNPicList;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.bean.YNUser;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNCommentResponse;
import com.github.postsanf.yinian.entity.YNCommonResponse;
import com.github.postsanf.yinian.fragment.MineFragement;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.TimeUtils;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.utils.YNInviteSecret;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.github.postsanf.yinian.view.DetailRecyclerView;
import com.github.postsanf.yinian.widget.MyJCVideoPlayerStandard;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;


public class TimelineImgDetailActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout status_detail_content;
    private TextView top_back_time;
    private LinearLayout main_video;
    private MyJCVideoPlayerStandard main_video_detail_singevideo;

    private SimpleDraweeView top_item_avatar;
    private ImageView top_item_type;
    private TextView top_item_name;
    private TextView top_item_time;
    private LinearLayout ll_top_item_imgtip;
    private TextView top_item_imgtip;
    private TextView top_item_imgnum;
    private TextView top_item_tiptype;
    private LinearLayout main_img;

    private LinearLayout main_text;
    private TextView main_text_econtent;

    private LinearLayout main_mic;
    private TextView main_mic_etext;
    private TextView main_mic_etext_tip;
    private RelativeLayout main_mic_mic;

    private LinearLayout main_location;
    private TextView main_location_etext;
    private ImageView main_location_pic;
    private SimpleDraweeView iv_detail_content;

    private RecyclerView mRecyclerView;
    private TimeLineDetialAdapter mAdapter;
    private List<YNComment> mCommentList = new ArrayList<>();
    private DetailRecyclerView dRecyclerView;
    private DetailAdapter dAdapter;
    private YNStatus mCurStatus;
    private int groupid;
    private List<String> mUrls = new ArrayList<>();
    private View topView;

    private LinearLayout item_img;
    private SimpleDraweeView item_img_detail;
    private DetailRecyclerView item_img_detail_horizontal;
    private LinearLayout ll_status_detail_content;
    private TextView tv_status_detail_content;

    private TextView item_etext;
    private LinearLayout timeline_hix;
    private RelativeLayout item_mic;
    private TextView item_etext_tip;

    private Button btn_comment;
    private EditText et_comment;
    private boolean isSending = false;//  是否在发布评论  默认 否
    private String cContent;
    private YNUser commentedUser;
    private AlertDialog alertDialog;
    private YNComment mCommentItem;
    private YNUser sys = new YNUser();
    private int pageIndex = 0;
    private LinearLayout item_menu;
    private ImageView item_menu_face;
    private TextView item_menu_facenum;
    private RecyclerView zan_recycler_horizontal;
    private ZanHistoryAdapter zanHistoryAdapter;
    private PopShareTimeLine mpopShareTimeLine;
    private PopupWindow window;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_timeline_img_detail);
        initDatas();
        initView();
    }


    private void initDatas() {
        mCurStatus = YNSingleton.getInstance().getCurStatus();
        groupid = application.getCurGid();
        mCommentList = mCurStatus.getComments();
        sys.setUserid(CommonConstants.SYSTEM_ID);
        commentedUser = sys;
    }


    private View.OnClickListener itemsOnClick2 = new View.OnClickListener() {
        public void onClick(View v) {
            mpopShareTimeLine.dismiss();
            switch (v.getId()) {
                case R.id.btn_setuser_take_photo:
                    Intent pintent = new Intent(TimelineImgDetailActivity.this, SharePhotoActivity.class);
                    ArrayList<String> urls = new ArrayList<>();
                    urls.clear();
                    for (int i = 0; i < mCurStatus.getPicList().size(); i++) {
                        urls.add(mCurStatus.getPicList().get(i).getThumbnail());
                    }
                    pintent.putStringArrayListExtra(ACTIONs.aAddData, urls);
                    startActivity(pintent);
                    break;
                case R.id.btn_setuser_pick_photo:
                    String url = "http://wx.zhuiyinanian.com/app/sharedetail.html?code=";
                    String sec = null;
                    try {
                        sec = YNInviteSecret.encryptDES("eid=" + mCurStatus.getEid(), getResources().getString(R.string.UNKNOW_ID));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    url = url + sec;
                    Intent sintent = new Intent(TimelineImgDetailActivity.this, LineTimeStatusShareActivity.class);
                    sintent.putExtra("UrlShare", url);
                    sintent.putExtra("title", mCurStatus.getPublishUser().getUnickname() + "有一段美好时光@你");
                    sintent.putExtra("desc", "喊你进ta的时光隧道看看啦");
                    sintent.putExtra("avatarPaths", MineFragement.avatarPic);
                    startActivity(sintent);
                    break;
                case R.id.btn_setuser_cancel_photo:
                    break;
            }
        }
    };

    private View.OnTouchListener itemsOnTouch2 = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int height = mpopShareTimeLine.mMenuView.findViewById(R.id.pop_setuser_advatar_layout).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    mpopShareTimeLine.dismiss();
                }
            }
            return true;
        }
    };

    private void initView() {
        findViewById(R.id.top_back).setOnClickListener(this);
        status_detail_content = (RelativeLayout) findViewById(R.id.status_detail_content);
        top_back_time = (TextView) findViewById(R.id.top_back_time);
        findViewById(R.id.statusdetail_more).setOnClickListener(this);
        topView = View.inflate(this, R.layout.yn_include_timelineimg_detail_top, null);
        top_item_avatar = (SimpleDraweeView) topView.findViewById(R.id.top_item_avatar);
        top_item_type = (ImageView) topView.findViewById(R.id.top_item_type);
        top_item_name = (TextView) topView.findViewById(R.id.top_item_name);
        top_item_time = (TextView) topView.findViewById(R.id.top_item_time);
        ll_top_item_imgtip = (LinearLayout) topView.findViewById(R.id.ll_top_item_imgtip);
        top_item_imgtip = (TextView) topView.findViewById(R.id.top_item_imgtip);
        top_item_imgnum = (TextView) topView.findViewById(R.id.top_item_imgnum);
        top_item_tiptype = (TextView) topView.findViewById(R.id.top_item_tiptype);
        mpopShareTimeLine = new PopShareTimeLine(TimelineImgDetailActivity.this, itemsOnClick2, itemsOnTouch2);
        dAdapter = new DetailAdapter(this, mUrls);
        //视频为主元素
        main_video = (LinearLayout) topView.findViewById(R.id.main_video);
        main_video_detail_singevideo = (MyJCVideoPlayerStandard) topView.findViewById(R.id.main_video_detail_singevideo);
        //图片为主元素
        main_img = (LinearLayout) topView.findViewById(R.id.main_img);
        iv_detail_content = (SimpleDraweeView) topView.findViewById(R.id.main_img_detail_content);
        dRecyclerView = (DetailRecyclerView) topView.findViewById(R.id.main_img_detail_horizontal);
        dRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        dRecyclerView.setHasFixedSize(true);
        dRecyclerView.setAdapter(dAdapter);

        main_text = (LinearLayout) topView.findViewById(R.id.main_text);
        main_text_econtent = (TextView) topView.findViewById(R.id.main_text_econtent);
        main_mic = (LinearLayout) topView.findViewById(R.id.main_mic);
        main_mic_etext = (TextView) topView.findViewById(R.id.main_mic_etext);
        main_mic_etext_tip = (TextView) topView.findViewById(R.id.main_mic_etext_tip);
        main_mic_mic = (RelativeLayout) topView.findViewById(R.id.main_mic_mic);
        main_location = (LinearLayout) topView.findViewById(R.id.main_location);
        main_location_etext = (TextView) topView.findViewById(R.id.main_location_etext);
        main_location_pic = (ImageView) topView.findViewById(R.id.main_location_pic);

        //赞列表
        item_menu = (LinearLayout) topView.findViewById(R.id.item_menu);
        item_menu_face = (ImageView) topView.findViewById(R.id.item_menu_face);
        item_menu_facenum = (TextView) topView.findViewById(R.id.item_menu_facenum);
        zan_recycler_horizontal = ((RecyclerView) topView.findViewById(R.id.zan_recycler_horizontal));
        zan_recycler_horizontal.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        item_img = (LinearLayout) topView.findViewById(R.id.item_img);
        item_img_detail = (SimpleDraweeView) topView.findViewById(R.id.item_img_detail);
        item_img_detail_horizontal = (DetailRecyclerView) topView.findViewById(R.id.item_img_detail_horizontal);
        item_img_detail_horizontal.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        item_img_detail_horizontal.setHasFixedSize(true);
        item_img_detail_horizontal.setAdapter(dAdapter);

        //内容
        ll_status_detail_content = (LinearLayout) topView.findViewById(R.id.ll_status_detail_content);
        tv_status_detail_content = (TextView) topView.findViewById(R.id.tv_status_detail_content);

        timeline_hix = (LinearLayout) topView.findViewById(R.id.timeline_hix);
        item_etext = (TextView) topView.findViewById(R.id.item_etext);
        item_mic = (RelativeLayout) topView.findViewById(R.id.item_mic);
        item_etext_tip = (TextView) topView.findViewById(R.id.item_etext_tip);
        if (mCurStatus == null)
            return;
        List<YNPicList> Ynpiclists = mCurStatus.getPicList();
        if (Ynpiclists != null && Ynpiclists.size() > 0) {
            top_item_imgnum.setText(String.valueOf(Ynpiclists.size()));
            ll_top_item_imgtip.setVisibility(View.VISIBLE);
        } else {
            ll_top_item_imgtip.setVisibility(View.INVISIBLE);
        }
        String dateStr = mCurStatus.getEuploadtime();
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).parse(dateStr);
            String InMouth = DateFormat.format("MM", date).toString();
            String Indd = DateFormat.format("dd", date).toString();
            top_back_time.setText(DateFormat.format("yyyy", date).toString() + "/" + InMouth + "/" + Indd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        top_item_time.setText(TimeUtils.getStatusTime(dateStr));
        ImageOptHelper.showFrescoAvatar(Uri.parse(YNUrlHelper.getAvatarUrl(this, mCurStatus.getPublishUser().getUpic())), top_item_avatar, this);
        top_item_name.setText(mCurStatus.getPublishUser().getUnickname());
        switch (mCurStatus.geteMain()) {
            case 4:
                top_item_imgtip.setText("分享了");
                top_item_tiptype.setText(" 段视频");
                top_item_type.setImageResource(R.drawable.timeline_type_video);
                main_video.setVisibility(View.VISIBLE);
                if (Ynpiclists != null && Ynpiclists.size() > 0) {
                    if (Ynpiclists.size() == 1) {
                        main_video_detail_singevideo.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIHelper.getVideoSingleHigh(TimelineImgDetailActivity.this)));
                        main_video_detail_singevideo.setUp(Ynpiclists.get(0).getPoriginal(), "");
                        if (!StringUtils.isEmpty(Ynpiclists.get(0).getPcover())) {
                            Glide.with(TimelineImgDetailActivity.this).load(Ynpiclists.get(0).getPcover()).into(main_video_detail_singevideo.ivThumb);
                        } else {
                            Glide.with(TimelineImgDetailActivity.this).load(R.drawable.item_singevideo_deaut).into(main_video_detail_singevideo.ivThumb);
                        }
                    }
                }
                break;
            case 0:
                top_item_imgtip.setText("分享了");
                top_item_tiptype.setText(" 张照片");
                top_item_type.setImageResource(R.drawable.timeline_type_img);
                main_img.setVisibility(View.VISIBLE);
                if (Ynpiclists != null && Ynpiclists.size() > 0) {
                    dRecyclerView.setOnItemScrollChangeListener(new DetailRecyclerView.OnItemScrollChangeListener() {
                        @Override
                        public void onChange(View view, int position) {
                            if (position == -1) {
                                position = 0;
                            }
                            pageIndex = position;
                            String path = YNUrlHelper.getStatusUrl(TimelineImgDetailActivity.this, mUrls.get(position));
                            ImageOptHelper.showFrescoStatusSingle(Uri.parse(StringUtils.repaceImgUrl(path)), iv_detail_content, TimelineImgDetailActivity.this);
                        }

                    });
                    dAdapter.setOnItemClickLitener(new DetailAdapter.OnItemClickLitener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            pageIndex = position;
                            String path = YNUrlHelper.getStatusUrl(TimelineImgDetailActivity.this, mUrls.get(position));
                            ImageOptHelper.showFrescoStatusSingle(Uri.parse(StringUtils.repaceImgUrl(path)), iv_detail_content, TimelineImgDetailActivity.this);
                        }
                    });
                    iv_detail_content.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            YNSingleton.getInstance().setCurStatus(mCurStatus);
                            Intent intent = new Intent(TimelineImgDetailActivity.this, BrowseActivity.class);
                            intent.putExtra(CommonConstants.YNPOSITION, pageIndex);
                            intent.putExtra(CommonConstants.TIMELINE, true);
                            startActivity(intent);
                        }
                    });
                    for (int i = 0; i < Ynpiclists.size(); i++) {
                        mUrls.add(Ynpiclists.get(i).getPoriginal());
                    }
                    dAdapter.notifyDataSetChanged();
                    if (mUrls.size() > 0) {
                        String path = YNUrlHelper.getStatusUrl(TimelineImgDetailActivity.this, mUrls.get(0));
                        ImageOptHelper.showFrescoStatusSingle(Uri.parse(StringUtils.repaceImgUrl(path)), iv_detail_content, TimelineImgDetailActivity.this);
                    }
                }
//                if (StringUtils.isEmpty(mCurStatus.getEtext()) && StringUtils.isEmpty(mCurStatus.getEaudio())
//                        && StringUtils.isEmpty(mCurStatus.getEplace())
//                        && StringUtils.isEmpty(mCurStatus.getePeopleName())
//                        && (mCurStatus.getComments() == null || mCurStatus.getComments().size() == 0)
//                        && (mCurStatus.getLike() == null || mCurStatus.getLike().size() == 0)) {
//                    jianjiao.setVisibility(View.GONE);
//                } else {
//                    jianjiao.setVisibility(View.VISIBLE);
//                }
                break;
            case 1:
                top_item_imgtip.setText("附带了");
                top_item_type.setImageResource(R.drawable.timeline_type_text);
                main_text.setVisibility(View.VISIBLE);
                main_text_econtent.setText(mCurStatus.getEtext());
                break;
            case 2:
                top_item_imgtip.setText("附带了");
                top_item_type.setImageResource(R.drawable.timeline_type_mic);
                main_mic.setVisibility(View.VISIBLE);
                main_mic_mic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(TimelineImgDetailActivity.this, MicPlayActivity.class);
                        intent.putExtra("Eaudio", mCurStatus.getEaudio());
                        intent.putExtra("ePeopleName", mCurStatus.getePeopleName());
                        intent.putExtra("Eplace", mCurStatus.getEplace());
                        startActivity(intent);
                    }
                });
                if (!StringUtils.isEmpty(mCurStatus.getePeopleName())) {
                    main_mic_etext_tip.setVisibility(View.GONE);
                    if (!StringUtils.isEmpty(mCurStatus.getEplace())) {
                        main_mic_etext.setText(UIHelper.getTextHtmlMic1(this, mCurStatus.getePeopleName(), mCurStatus.getEplace()));
                    } else {
                        main_mic_etext.setText(UIHelper.getTextHtmlMic2(this, mCurStatus.getePeopleName()));
                    }
                } else {
                    if (!StringUtils.isEmpty(mCurStatus.getEplace())) {
                        main_mic_etext.setText(UIHelper.getTextHtmlMic3(this, mCurStatus.getEplace()));
                    } else {
                        main_mic_etext_tip.setVisibility(View.VISIBLE);
                    }
                }
                break;
            case 3:
                top_item_imgtip.setText("附带了");
                top_item_type.setImageResource(R.drawable.timeline_type_location);
                main_location.setVisibility(View.VISIBLE);
                if (!StringUtils.isEmpty(mCurStatus.getePeopleName())) {
                    main_location_etext.setText(UIHelper.getTextHtmlLocation1(this, mCurStatus.getePeopleName(), mCurStatus.getEplace()));
                } else {
                    main_location_etext.setText(UIHelper.getTextHtmlLocation2(this, mCurStatus.getEplace()));
                }
                String paths = YNUrlHelper.getStatusUrl(TimelineImgDetailActivity.this, mCurStatus.getePlacePic());
                ImageOptHelper.showFrescoStatus(StringUtils.repaceImgUrl(paths), main_location_pic, TimelineImgDetailActivity.this);
                break;
        }
        item_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View vv = v.findViewById(R.id.item_menu_face);
                popAwindow(vv);
            }
        });
        List<YNLike> likeList = mCurStatus.getLike();
        zanHistoryAdapter = new ZanHistoryAdapter(TimelineImgDetailActivity.this, mCurStatus);
        zan_recycler_horizontal.setAdapter(zanHistoryAdapter);
        if (likeList != null && likeList.size() > 0) {
            item_menu_facenum.setText(String.valueOf(likeList.size()));
            item_menu_facenum.setVisibility(View.VISIBLE);
            if (juideIsContionMe(likeList)) {
                for (int i = 0; i < likeList.size(); i++) {
                    if (likeList.get(i).getLikeUserID() == application.getCurID()) {
                        setFace(likeList.get(i).getLikeStatus(), item_menu_face);
                    }
                }
            } else {
                item_menu_face.setImageResource(R.drawable.timeline_zan_before);
            }
            zanHistoryAdapter.notifyDataSetChanged();
        } else {
            item_menu_facenum.setVisibility(View.GONE);
        }
        if (mCurStatus.geteMain() != 0 && mCurStatus.geteMain() != 4) {
            item_img.setVisibility(View.VISIBLE);
            if (Ynpiclists != null && Ynpiclists.size() > 0) {
                item_img_detail_horizontal.setOnItemScrollChangeListener(new DetailRecyclerView.OnItemScrollChangeListener() {
                    @Override
                    public void onChange(View view, int position) {
                        if (position == -1) {
                            position = 0;
                        }
                        pageIndex = position;
                        String path = YNUrlHelper.getStatusUrl(TimelineImgDetailActivity.this, mUrls.get(position));
                        ImageOptHelper.showFrescoStatusSingle(Uri.parse(StringUtils.repaceImgUrl(path)), item_img_detail, TimelineImgDetailActivity.this);
                    }
                });
                dAdapter.setOnItemClickLitener(new DetailAdapter.OnItemClickLitener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        pageIndex = position;
                        String path = YNUrlHelper.getStatusUrl(TimelineImgDetailActivity.this, mUrls.get(position));
                        ImageOptHelper.showFrescoStatusSingle(Uri.parse(StringUtils.repaceImgUrl(path)), item_img_detail, TimelineImgDetailActivity.this);
                    }
                });
                item_img_detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        YNSingleton.getInstance().setCurStatus(mCurStatus);
                        Intent intent = new Intent(TimelineImgDetailActivity.this, BrowseActivity.class);
                        intent.putExtra(CommonConstants.YNPOSITION, pageIndex);
                        intent.putExtra(CommonConstants.TIMELINE, true);
                        startActivity(intent);
                    }
                });
                for (int i = 0; i < Ynpiclists.size(); i++) {
                    mUrls.add(Ynpiclists.get(i).getPoriginal());
                }
                dAdapter.notifyDataSetChanged();
                if (mUrls.size() > 0) {
                    String path = YNUrlHelper.getStatusUrl(TimelineImgDetailActivity.this, mUrls.get(0));
                    ImageOptHelper.showFrescoStatusSingle(Uri.parse(StringUtils.repaceImgUrl(path)), item_img_detail, TimelineImgDetailActivity.this);
                }
            } else {
                item_img.setVisibility(View.GONE);
            }
        } else {
            item_img.setVisibility(View.GONE);
        }
        if (mCurStatus.geteMain() != 1) {
            if (!StringUtils.isEmpty(mCurStatus.getEtext())) {
                ll_status_detail_content.setVisibility(View.VISIBLE);
                tv_status_detail_content.setText(mCurStatus.getEtext());
            } else {
                tv_status_detail_content.setText("");
                ll_status_detail_content.setVisibility(View.GONE);
            }
        } else {
            ll_status_detail_content.setVisibility(View.GONE);
        }
        if (mCurStatus.geteMain() != 2) {
            item_mic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(TimelineImgDetailActivity.this, MicPlayActivity.class);
                    intent.putExtra("Eaudio", mCurStatus.getEaudio());
                    intent.putExtra("ePeopleName", mCurStatus.getePeopleName());
                    intent.putExtra("Eplace", mCurStatus.getEplace());
                    startActivity(intent);
                }
            });
            if (mCurStatus.geteMain() != 3) {
                //语音
                if (!StringUtils.isEmpty(mCurStatus.getEaudio())) {
                    item_mic.setVisibility(View.VISIBLE);
                } else {
                    item_mic.setVisibility(View.GONE);
                }
                //和谁，地点
                if (!StringUtils.isEmpty(mCurStatus.getePeopleName())) {
                    timeline_hix.setVisibility(View.VISIBLE);
                    item_etext_tip.setVisibility(View.GONE);
                    if (!StringUtils.isEmpty(mCurStatus.getEplace())) {
                        if (!StringUtils.isEmpty(mCurStatus.getEaudio())) {
                            item_etext.setText(UIHelper.getTextHtmlMic1(this, mCurStatus.getePeopleName(), mCurStatus.getEplace()));
                        } else {
                            item_etext.setText(UIHelper.getTextHtmlfuMic1(this, mCurStatus.getePeopleName(), mCurStatus.getEplace()));
                        }
                    } else {
                        if (!StringUtils.isEmpty(mCurStatus.getEaudio())) {
                            item_etext.setText(UIHelper.getTextHtmlfuMic2(this, mCurStatus.getePeopleName()));
                        } else {
                            item_etext.setText(UIHelper.getTextHtmlfuMic3(this, mCurStatus.getePeopleName()));
                        }
                    }
                } else {
                    timeline_hix.setVisibility(View.VISIBLE);
                    if (!StringUtils.isEmpty(mCurStatus.getEplace())) {
                        timeline_hix.setVisibility(View.VISIBLE);
                        if (!StringUtils.isEmpty(mCurStatus.getEaudio())) {
                            item_etext.setText(UIHelper.getTextHtmlMic3(this, mCurStatus.getEplace()));
                        } else {
                            item_etext.setText(UIHelper.getTextHtmlLocation2(this, mCurStatus.getEplace()));
                        }
                    } else {
                        timeline_hix.setVisibility(View.VISIBLE);
                        if (!StringUtils.isEmpty(mCurStatus.getEaudio())) {
                            item_etext_tip.setVisibility(View.VISIBLE);
                        } else {
                            timeline_hix.setVisibility(View.GONE);
                            item_etext_tip.setVisibility(View.GONE);
                        }
                    }
                }
            } else {
                item_etext_tip.setVisibility(View.GONE);
                timeline_hix.setVisibility(View.GONE);
            }
        } else {
            item_etext_tip.setVisibility(View.GONE);
            timeline_hix.setVisibility(View.GONE);
        }
        btn_comment = (Button) findViewById(R.id.btn_comment);
        et_comment = (EditText) findViewById(R.id.et_comment);
        btn_comment.setOnClickListener(this);
        //评论列表
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_status_detail_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mAdapter = new TimeLineDetialAdapter(this, mCommentList, groupid);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setHeaderView(topView);
        if (mCommentList != null && mCommentList.size() > 0) {
            mRecyclerView.scrollToPosition(1);
        }
        //为每个item增加响应事件
        mAdapter.setOnItemClickListener(new TimeLineDetialAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, YNComment data) {
                mCommentItem = data;
                if (mCommentItem.getCommentUser().getUserid() == application.getCurID() || String.valueOf(application.getCurID()).equals(getString(R.string.numberofme))) {
                    popDelComment();
                } else {
                    publishComment();
                }
            }
        });
    }

    private void popAwindow(View v) {
        final PopMenuTimeLineTop mPopMenu = new PopMenuTimeLineTop(TimelineImgDetailActivity.this, UIHelper.dip2px(TimelineImgDetailActivity.this, 230),
                UIHelper.dip2px(TimelineImgDetailActivity.this, 60));
        mPopMenu.show(v);
        mPopMenu.getContentView().findViewById(R.id.timeline_face1).setOnClickListener(new MyZanOnClickListener(mCurStatus.getEid(), "2", mPopMenu));
        mPopMenu.getContentView().findViewById(R.id.timeline_face2).setOnClickListener(new MyZanOnClickListener(mCurStatus.getEid(), "4", mPopMenu));
        mPopMenu.getContentView().findViewById(R.id.timeline_face3).setOnClickListener(new MyZanOnClickListener(mCurStatus.getEid(), "5", mPopMenu));
        mPopMenu.getContentView().findViewById(R.id.timeline_face4).setOnClickListener(new MyZanOnClickListener(mCurStatus.getEid(), "3", mPopMenu));
        mPopMenu.getContentView().findViewById(R.id.timeline_face5).setOnClickListener(new MyZanOnClickListener(mCurStatus.getEid(), "0", mPopMenu));
    }

    private boolean juideIsContionMe(List<YNLike> likeList) {
        for (int i = 0; i < likeList.size(); i++) {
            if (likeList.get(i).getLikeUserID() == application.getCurID()) {
                return true;
            }
        }
        return false;
    }

    public void setFace(int LikeStatus, ImageView avatartype) {
        switch (LikeStatus) {
            case 2:
                avatartype.setImageResource(R.drawable.timeline_face1);
                break;
            case 4:
                avatartype.setImageResource(R.drawable.timeline_face2);
                break;
            case 5:
                avatartype.setImageResource(R.drawable.timeline_face3);
                break;
            case 3:
                avatartype.setImageResource(R.drawable.timeline_face4);
                break;
            case 0:
                avatartype.setImageResource(R.drawable.timeline_face5);
                break;
        }
    }


    private void popDelComment() {
        View sexView = View.inflate(this, R.layout.yn_comment_del, null);
        LinearLayout ll_ret = (LinearLayout) sexView.findViewById(R.id.ll_comment_ret);
        LinearLayout ll_del = (LinearLayout) sexView.findViewById(R.id.ll_comment_del);
        alertDialog = new AlertDialog.Builder(this).show();
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.setContentView(sexView);
        WindowManager.LayoutParams layout = alertDialog.getWindow().getAttributes();
        layout.width = DisplayUtils.getScreenWidthPixels(this) - DisplayUtils.dp2px(this, 32 * 2);
        alertDialog.getWindow().setAttributes(layout);
        ll_ret.setVisibility(View.GONE);
//        // 回复
//        ll_ret.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                publishComment();
//                alertDialog.dismiss();
//            }
//        });
        // 删除
        ll_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delComment(mCommentItem.getCid());
                alertDialog.dismiss();
            }
        });
    }

    // 删除评论
    private void delComment(final String comId) {
        if (StringUtils.isEmptyString(comId)) {
            showToast("删除失败");
            return;
        }
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_DEL_COMMENT, comId);
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynDelComment)).setMethod(HttpMethods.Post).setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCommonResponse resDelComment = gson.fromJson(s, YNCommonResponse.class);
                        if (resDelComment != null && ReqUtils.isSuccess(resDelComment.getCode())) {
                            for (int i = 0; i < mCurStatus.getComments().size(); i++) {
                                if (comId.equals(mCurStatus.getComments().get(i).getCid())) {
                                    mCurStatus.getComments().remove(i);
                                    mAdapter.notifyItemRemoved(i + 1);
//                                    mRecyclerView.scrollToPosition(i);
                                    // 通知评论更新
                                    Intent mIntent = new Intent(ACTIONs.actionComment);
                                    sendBroadcast(mIntent);
                                }
                            }

                        } else {
                            showToast(resDelComment.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void publishComment() {
        commentedUser = mCommentItem.getCommentUser();
        et_comment.setHint("//回复" + mCommentItem.getCommentUser().getUnickname() + ":");
        et_comment.setFocusable(true);
        et_comment.setFocusableInTouchMode(true);
        et_comment.requestFocus();
        et_comment.findFocus();
    }

    // 发表评论
    private void doComment() {
        if (checkComment() && !isSending) {
            isSending = true;// 更改状态
            LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
            mParam.put(CommonConstants.YNS_COMMENT_USER_ID, String.valueOf(application.getCurID()));
            mParam.put(CommonConstants.YNS_COMMENTED_USER_ID, String.valueOf(commentedUser.getUserid()));
            mParam.put(CommonConstants.YNS_EVENT_ID, String.valueOf(mCurStatus.getEid()));
            mParam.put(CommonConstants.YNS_PLACE, MainActivity.LocationCity);
            mParam.put(CommonConstants.YNS_CONTENT, cContent);
            StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynSendComment)).setMethod(HttpMethods.Post).setParamMap(mParam)
                    .setHttpListener(new HttpListener<String>(true, false, true) {
                        @Override
                        public void onSuccess(String s, Response<String> response) {
                            YNCommentResponse resSendComment = gson.fromJson(s, YNCommentResponse.class);
                            if (resSendComment != null && ReqUtils.isSuccess(resSendComment.getCode())) {
                                YNComment createComment = resSendComment.getData().get(0);
                                createComment.setCcontent(cContent);
                                application.getCurUser().setUserid(application.getCurID());
                                createComment.setCommentUser(application.getCurUser());
                                createComment.setCommentedUser(commentedUser);
                                createComment.setCtime(DateFormat.format("yyyy-MM-dd  HH:mm:ss", new Date()).toString());
                                mCurStatus.getComments().add(mCurStatus.getComments().size(), createComment);
                                mAdapter.notifyItemInserted(mCurStatus.getComments().size());
                                mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
                                // 通知评论更新
                                sendBroadcast(new Intent(ACTIONs.actionComment));
                                et_comment.setHint("对ta说点啥");
                                et_comment.setText("");
                                // 还原当前对系统的评论
                                commentedUser = sys;
                            } else {
                                showToast(resSendComment.getMsg());
                            }
                            // 还原状态
                            isSending = false;
                        }

                        @Override
                        public void onFailure(HttpException e, Response<String> response) {
                            showToast(CommonConstants.YN_NET_LINK);
                            // 还原状态
                            isSending = false;
                        }
                    });
            liteHttp.executeAsync(req);
        }
    }

    // 检测评论
    private Boolean checkComment() {
        cContent = et_comment.getText().toString().trim();
        if (StringUtils.isEmptyString(cContent)) {
            return false;
        }
        return true;
    }


    public void ToDelDialog() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        builder.setCancelable(false);
        final android.app.AlertDialog dlg = builder.create();
        dlg.show();
        Window window = dlg.getWindow();
        window.setContentView(R.layout.pub_dialog);
        window.setBackgroundDrawableResource(R.color.transparent);
        WindowManager.LayoutParams layout = dlg.getWindow().getAttributes();
        layout.width = DisplayUtils.getScreenWidthPixels(this) - DisplayUtils.dp2px(this, 35 * 2);
        dlg.getWindow().setAttributes(layout);
        TextView tt = (TextView) window.findViewById(R.id.pp_title);
        tt.setText("提示");
        TextView content = (TextView) window.findViewById(R.id.pp_content);
        content.setText("确定要删除动态详情吗?");
        TextView cancel = (TextView) window
                .findViewById(R.id.btn_cancel);
        cancel.setText("取消");
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dlg.cancel();
            }
        });
        final TextView ok = (TextView) window.findViewById(R.id.btn_ok);
        ok.setText("确定");
        ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dlg.cancel();
                doDeleteStatus();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_comment:
                doComment();
                break;
            case R.id.top_back:
                finish();
                break;
            case R.id.statusdetail_more:
                showPP(v);
                break;
            case R.id.pp_delete:
                window.dismiss();
                ToDelDialog();
                break;
            case R.id.pp_share:
                window.dismiss();
                if (getIntent() != null && getIntent().getBooleanExtra("fromfrist", false)) {
                    String url = "http://wx.zhuiyinanian.com/app/shareactivitydetail.html?code=";
                    String sec = null;
                    try {
                        sec = YNInviteSecret.encryptDES("eid=" + mCurStatus.getEid(), getResources().getString(R.string.UNKNOW_ID));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    url = url + sec;
                    Intent sintent = new Intent(this, LineTimeStatusFristShareActivity.class);
                    sintent.putExtra("UrlShare", url);
                    sintent.putExtra("title", TimeLineStatusFristActivity.ShareTitle);
                    sintent.putExtra("desc", TimeLineStatusFristActivity.ShareContent);
                    sintent.putExtra("avatarPaths", MineFragement.avatarPic);
                    startActivity(sintent);
                } else {
                    if (mCurStatus.getPicList() != null && mCurStatus.getPicList().size() > 0) {
                        mpopShareTimeLine.showAtLocation(status_detail_content, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                    } else {
                        String url = "http://wx.zhuiyinanian.com/app/sharedetail.html?code=";
                        String sec = null;
                        try {
                            sec = YNInviteSecret.encryptDES("eid=" + mCurStatus.getEid(), getResources().getString(R.string.UNKNOW_ID));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        url = url + sec;
                        Intent sintent = new Intent(this, LineTimeStatusShareActivity.class);
                        sintent.putExtra("UrlShare", url);
                        sintent.putExtra("title", "忆年");
                        sintent.putExtra("desc", "来自忆年分享");
                        startActivity(sintent);
                    }
                }
                break;
        }
    }

    protected void showPP(View parent) {
        View v = LayoutInflater.from(TimelineImgDetailActivity.this).inflate(R.layout.timeline_detail_pp,
                null);
        View pp_line1 = v.findViewById(R.id.pp_line1);
        TextView delete = (TextView) v.findViewById(R.id.pp_delete);
        delete.setOnClickListener(this);
        TextView share = (TextView) v.findViewById(R.id.pp_share);
        share.setOnClickListener(this);
        boolean userStatus = getIntent().getBooleanExtra("userStatus", false);
        if (mCurStatus.getPublishUser() != null && mCurStatus.getPublishUser().getUserid() == application.getCurID() || userStatus) {
            window = new PopupWindow(v, UIHelper.dip2px(TimelineImgDetailActivity.this, 80), UIHelper.dip2px(TimelineImgDetailActivity.this, 81));
        } else {
            delete.setVisibility(View.GONE);
            pp_line1.setVisibility(View.GONE);
            window = new PopupWindow(v, UIHelper.dip2px(TimelineImgDetailActivity.this, 80), UIHelper.dip2px(TimelineImgDetailActivity.this, 41));
        }
        window.setAnimationStyle(R.style.pp_AnimationPreview);
        window.setTouchable(true);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new BitmapDrawable());
        window.showAsDropDown(parent);
    }

    // 删除该条动态
    private void doDeleteStatus() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_EVENT_ID, String.valueOf(mCurStatus.getEid()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynDeleteEvent))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCommonResponse resDelete = gson.fromJson(s, YNCommonResponse.class);
                        if (ReqUtils.isSuccess(resDelete.getCode())) {
                            Intent mIntent = new Intent(ACTIONs.actionStatus);
                            // 同步删除动态
                            mIntent.putExtra(ACTIONs.IsAdd, false);
                            mIntent.putExtra(ACTIONs.StatusId, mCurStatus.getEid());
                            sendBroadcast(mIntent);
                            sendBroadcast(new Intent(ACTIONs.actionGridWallUpdate));
                            finish();
                            if (getIntent().getBooleanExtra("detail", false)) {
                                sendBroadcast(new Intent(ACTIONs.McardDetailFinish));
                            }
                        } else {
                            showToast("删除失败");
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }


    public class MyZanOnClickListener implements View.OnClickListener {
        int eid;
        String type;
        PopMenuTimeLineTop mPopMenu;

        public MyZanOnClickListener(int eid, String type, PopMenuTimeLineTop mPopMenu) {
            this.eid = eid;
            this.type = type;
            this.mPopMenu = mPopMenu;
        }

        @Override
        public void onClick(View v) {
            setFace(Integer.parseInt(type), item_menu_face);
            doClickZan(eid, type);
            mPopMenu.dismiss();
        }
    }

    private void doClickZan(int eid, final String type) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_EID, String.valueOf(eid));
        mParam.put(CommonConstants.YNS_TYPE, type);
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/yinian/AttachOrRemoveExpression").setMethod(HttpMethods.Post).setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s.toString());
                            if (jsonObject.optString("msg").equals("success") && jsonObject.optInt("code") == 0) {
                                List<YNLike> likes = mCurStatus.getLike();
                                if (likes != null && likes.size() > 0) {
                                    List<YNLike> likesnew = new ArrayList<>();
                                    YNLike ynLike = new YNLike();
                                    ynLike.setUpic(MineFragement.avatarPic);
                                    ynLike.setLikeStatus(Integer.parseInt(type));
                                    ynLike.setLikeUserID(application.getCurID());
                                    likesnew.add(ynLike);
                                    for (int j = 0; j < likes.size(); j++) {
                                        if (likes.get(j).getLikeUserID() != application.getCurID()) {
                                            likesnew.add(likes.get(j));
                                        }
                                    }
                                    mCurStatus.getLike().clear();
                                    mCurStatus.setLike(likesnew);
                                } else {
                                    YNLike ynLike = new YNLike();
                                    ynLike.setUpic(MineFragement.avatarPic);
                                    ynLike.setLikeStatus(Integer.parseInt(type));
                                    ynLike.setLikeUserID(application.getCurID());
                                    mCurStatus.getLike().add(ynLike);
                                }
                                zanHistoryAdapter = new ZanHistoryAdapter(TimelineImgDetailActivity.this, mCurStatus);
                                zan_recycler_horizontal.setAdapter(zanHistoryAdapter);
                                zanHistoryAdapter.notifyDataSetChanged();
                                item_menu_facenum.setText(String.valueOf(mCurStatus.getLike().size()));
                                item_menu_facenum.setVisibility(View.VISIBLE);
                                sendBroadcast(new Intent(ACTIONs.actionLike));
                                Toast.makeText(TimelineImgDetailActivity.this, "点赞成功", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        Toast.makeText(mContext, CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT).show();
                    }
                });
        LiteHttp.newApacheHttpClient(new HttpConfig(TimelineImgDetailActivity.this)).executeAsync(req);
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        main_video_detail_singevideo.releaseAllVideos();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

}
